package org.example.config;

import jdk.internal.instrumentation.Logger;
import lombok.extern.slf4j.Slf4j;
import org.example.component.LoginInterceptor;
import org.example.component.MyLocalResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//@EnableWebMvc //完全接管 Spring MVC 后，Spring Boot 对 Spring MVC 的自动配置将全部失效。
@Configuration
@Slf4j
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //当访问 “/” 或 “/index.html” 时，都直接跳转到登陆页面
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        //添加视图映射 main.html 指向 dashboard.html
        registry.addViewController("/main.html").setViewName("main");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        log.info("注册拦截器");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")
//拦截所有请求，包括静态资源文件
                .excludePathPatterns("/","/login","/index.html","/user/login","/css/**","/images/**", "/js/**", "/fonts/**"); //放行登录页，登陆操作，静态资源
 }

 }

